/**
 *招聘管理js
 */

var pageCurr;
var form;
$(function () {
    layui.use(['form', 'laydate', 'table'], function () {
        var table = layui.table;
        form = layui.form;

        tableIns = table.render({
            id: 'recruitId',
            elem: '#recruitList',
            url: '/recruit/admin/getRecruitList',
            method: 'get', //默认：get请求
            title: '招聘信息表',
            cellMinWidth: 80,
            page: true,
            toolbar: 'default', //开启工具栏，此处显示默认图标
            request: {
                pageName: 'currPage', //页码的参数名称，默认：pageNum
                limitName: 'pageSize' //每页数据量的参数名，默认：pageSize
            },
            response: {
                statusName: 'code', //数据状态的字段名称，默认：code
                statusCode: 0, //成功的状态码，默认：0
                countName: 'total', //数据总数的字段名称，默认：count
                dataName: 'datas' //数据列表的字段名称，默认：data
            },
            /**
             * 修改table.js 中的源码得到的自定义方法。解决多层json嵌套无法渲染表格的问题
             */
            beforeRender: function (res) {
                res.total = res.page.total;
            },
            cols: [[
                // {type: 'checkbox', width: '3%', fixed: 'left'}
                 {field: 'recruitDepart', title: '招聘部门', align: 'center', width: '8%'}
                , {field: 'recruitPosition', title: '招聘职位', align: 'center', width: '10%'}
                , {field: 'recruitLocation', title: '工作地点', align: 'center', width: '8%'}
                , {field: 'recruitTime', title: '发布日期', align: 'center', width: '10%'}
                , {field: 'recruitNumber', title: '招聘人数', align: 'center', width: '8%'}
                , {field: 'recruitExperience', title: '工作经验', align: 'center', width: '8%'}
                , {field: 'recruitDuty', title: '岗位职责', align: 'center', width: '16%'}
                , {field: 'recruitQualitication', title: '任职要求', align: 'center', width: '16%'}
                , {fixed: 'right', title: '操作', align: 'center', toolbar: '#optBar', width: '13%'}
            ]],
            done: function (res, curr, count) {
                pageCurr = curr;

            }
        });


        //监听工具条
        table.on('tool(recruitTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {

                // 删除
                deleteRecruit(data);
            } else if (obj.event === 'edit') {
                recruitEdit(data)
            }


        });

        //监听事件
        table.on('toolbar(recruitTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id),
                data = checkStatus.data; //获取选中的数据;
            switch (obj.event) {
                case 'add':
                    window.location.href = "/recruit/recruitPublish";
                    break;
                case 'update':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else if (data.length > 1) {
                        layer.msg('只能同时编辑一个');
                    } else {
                        recruitEdit(checkStatus.data[0]);
                    }
                    break;
                case 'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else if (data.length > 1) {
                        layer.msg('请勿同时删除多行数据');
                    } else {
                        // layer.alert('编辑 [id]：' + checkStatus.data[0].recruitId);
                        deleteRecruit(checkStatus.data[0]);
                    }
                    break;
            }
        });

        var form = layui.form
            , layer = layui.layer
            , laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#date'
        });

        //日期
        laydate.render({
            elem: '#date1'
        });
        //监听提交
        form.on('submit(formRecruit)', function (data) {

            //获取表单值
            var recruitDepart = data.field.department; //招聘部门
            var recruitPosition = data.field.position; //职位
            var recruitLocation = data.field.place;  //地点
            var recruitTime = data.field.date; //日期
            var recruitNumber = data.field.number; //人数
            var recruitExperience = data.field.experience; //经验
            var recruitDuty = data.field.recruitDu; //职责
            var recruitQualitication = data.field.recruitQua; //要求

            $.ajax({
                type: "POST",
                url: "/recruit/admin/addRecruit",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({
                    recruitDepart: recruitDepart,
                    recruitPosition: recruitPosition,
                    recruitLocation: recruitLocation,
                    recruitTime: recruitTime,
                    recruitNumber: recruitNumber,
                    recruitExperience: recruitExperience,
                    recruitDuty: recruitDuty,
                    recruitQualitication: recruitQualitication,
                }),
                datatype: 'json',
                error: function (data) {
                    layer.alert("发布失败");
                },
                success: function (res) {

                    if (res.code == 0) {
                        layer.alert("发布成功");
                    } else {
                        layer.alert("发布失败");
                    }
                }
            });

            return false;
        });

        form.on('submit(recruitEdit)', function (obj) {
            // layer.msg(JSON.stringify(obj.field));

            //获取表单值
            var recruitId = obj.field.id;
            var recruitDepart = obj.field.department1; //招聘部门
            var recruitPosition = obj.field.position1; //职位
            var recruitLocation = obj.field.place1;  //地点
            var recruitTime = obj.field.date1; //日期
            var recruitNumber = obj.field.number1; //人数
            var recruitExperience = obj.field.experience1; //经验
            var recruitDuty = obj.field.recruitDuty1; //职责
            var recruitQualitication = obj.field.recruitQua1; //要求

            $.ajax({
                type: "POST",
                url: "/recruit/admin/updateRecruitById",
                contentType: "application/json;charset=utf-8",
                async: true,
                data: JSON.stringify({
                    recruitId: recruitId,
                    recruitDepart: recruitDepart,
                    recruitPosition: recruitPosition,
                    recruitLocation: recruitLocation,
                    recruitTime: recruitTime,
                    recruitNumber: recruitNumber,
                    recruitExperience: recruitExperience,
                    recruitDuty: recruitDuty,
                    recruitQualitication: recruitQualitication
                }),
                datatype: 'json',
                error: function (data) {
                    layer.alert("更新失败");
                    layer.closeAll();
                },
                success: function (res) {
                    if (res.code == 0) {
                        layer.alert("更新成功",function(){
                            layer.closeAll();
                            load(obj);
                        });
                    } else {
                        layer.alert("更新失败");
                    }
                }
            });

            return false;
        });

        //自定义验证规则
        form.verify({
            department: function (value) {
                if (value.length < 1) {
                    return '您还没输入招聘部门呢';
                }
            }
            , position: function (value) {
                if (value.length < 1) {
                    return '您还没输入招聘职位呢';
                }
            }
            , place: function (value) {
                if (value.length < 1) {
                    return '您还没输入工作地点呢';
                }
            }
            , date: function (value) {
                if (value.length < 1) {
                    return '您还没输入发布时间呢';
                }
            }
            , recruitDu: function (value) {
                if (value.length < 1) {
                    return '您还没输入工作职责呢';
                }
            }
            , recruitQua: function (value) {
                if (value.length < 1) {
                    return '您还没输入任职要求呢';
                }
            }
            , number: [
                /^[1-9]+[0-9]*/
                , '招聘人数必须为正整数呀'
            ]
            ,department1: function (value) {
                if (value.length < 1) {
                    return '您还没输入招聘部门呢';
                }
            }
            , position1: function (value) {
                if (value.length < 1) {
                    return '您还没输入招聘职位呢';
                }
            }
            , place1: function (value) {
                if (value.length < 1) {
                    return '您还没输入工作地点呢';
                }
            }
            , date1: function (value) {
                if (value.length < 1) {
                    return '您还没输入发布时间呢';
                }
            }
            , recruitDu1: function (value) {
                if (value.length < 1) {
                    return '您还没输入工作职责呢';
                }
            }
            , recruitQua1: function (value) {
                if (value.length < 1) {
                    return '您还没输入任职要求呢';
                }
            }
            , number1: [
                /^[1-9]+[0-9]*/
                , '招聘人数必须为正整数呀'
            ]
        });
    });

});

//打开编辑框
function recruitEdit(data) {
    //查看招聘信息
    if (data == null) {
        $("#id").val("");
    } else {
        //回显数据
        $("#id").val(data.recruitId);
        $("#department1").val(data.recruitDepart);
        $("#position1").val(data.recruitPosition);
        $("#date1").val(data.recruitTime);
        $("#place1").val(data.recruitLocation);
        $("#number1").val(data.recruitNumber);
        $("#experience1").val(data.recruitExperience);
        $("#recruitDuty1").val(data.recruitDuty);
        $("#recruitQua1").val(data.recruitQualitication);
    }

    layer.open({
        type: 1,
        title: "编辑招聘信息",
        fixed: false,
        resize: false,
        shadeClose: true,
        area: ['900px'],
        content: $('#setRecruit'),
        end:function(){
            cleanRecruit();
        }
    });

}


//重新加载table
function load(obj) {
    tableIns.reload({
        where: obj.field
        , page: {
            curr: pageCurr //从当前页码开始
        }
    });
}

function deleteRecruit(data) {
    //删除
    if (null != data.recruitId) {
        layer.confirm('您确定要删除吗？', {
            btn: ['确认', '取消'] //按钮
        }, function () {
            $.post("/recruit/admin/deleteRecruit", {"recruitId": data.recruitId}, function (data) {
                if (data.code == 0) {
                    layer.alert("删除成功", function () {
                        layer.closeAll();
                        load(data); //刷新
                    });
                } else {
                    layer.alert("删除失败");
                }
            });
        }, function () {
            layer.closeAll();
        });
    }
}

function cleanRecruit() {
    //回显数据
    $("#id").val("");
    $("#department1").val("");
    $("#position1").val("");
    $("#date1").val("");
    $("#place1").val("");
    $("#number1").val("");
    $("#experience1").val("");
    $("#recruitDuty1").val("");
    $("#recruitQua1").val("");
}






